package cn.itrip.auth.controller;

import cn.itrip.auth.service.UserService;
import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.vo.userinfo.ItripUserVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.ErrorCode;
import cn.itrip.common.MD5;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.regex.Pattern;

/**
 * @Author: jxm
 * @Description:用户控制器
 * @Date: 2020/3/11 23:51
 * @Version: 1.0
 */
@Controller
@RequestMapping("/api")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户使用邮箱注册
     * @param itripUserVO
     * @return
     */
    @RequestMapping(value="/doRegisterByEmail",method = RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Dto doRegisterByEmail(@RequestBody ItripUserVO itripUserVO){
        //1、邮箱验证
        if(!this.validEmail(itripUserVO.getUserCode())){
            return DtoUtil.returnFail("请使用正确的邮箱地址",ErrorCode.AUTH_ILLEGAL_USERCODE);
        }
        ItripUser user = new ItripUser();
        user.setUserCode(itripUserVO.getUserCode());
        user.setUserName(itripUserVO.getUserName());
        //user.setUserPassword(itripUserVO.getUserPassword());
        try {
            //验证用户
            if(null !=userService.findUserByUserCode( user.getUserCode())){
                return DtoUtil.returnFail("用户已经存在",ErrorCode.AUTH_USER_ALREADY_EXISTS);
            }else {
                //添加用户
                user.setUserPassword(MD5.getMd5(itripUserVO.getUserPassword(),32));
                userService.itriptxCreateUser(user);
                return DtoUtil.returnSuccess();
            }

        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("注册失败",ErrorCode.AUTH_UNKNOWN);
        }

    }

    /**
     * 激活
     * @param userCode
     * @param code
     * @return
     */
    @RequestMapping(value="/activate",method = RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Dto activate(@RequestParam String userCode, @RequestParam String code){

        try {
            if(userService.activate(userCode,code)){
                return DtoUtil.returnSuccess("激活成功");
            }else{
                return DtoUtil.returnSuccess("激活失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("激活失败",ErrorCode.AUTH_UNKNOWN);
        }
    }

    /**
     * 用户使用手机注册
     * @param vo
     * @return
     */
    @RequestMapping(value="/doRegisterByPhone",method = RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Dto doRegisterByPhone(@RequestBody ItripUserVO vo){
        //手机验证
        if(!validPhone(vo.getUserCode())){
            return DtoUtil.returnFail("请使用正确的手机号码",ErrorCode.AUTH_ILLEGAL_USERCODE);
        }
        try {
            if(null == userService.findUserByUserCode(vo.getUserCode())){
                ItripUser user = new ItripUser();
                user.setUserName(vo.getUserName());
                user.setUserCode(vo.getUserCode());
                user.setUserPassword(MD5.getMd5(vo.getUserPassword(),32));
                //调用service
                userService.itriptxCreateUserByPhone(user);
                return DtoUtil.returnSuccess();
            }else{
                return DtoUtil.returnFail("号码已经注册",ErrorCode.AUTH_USER_ALREADY_EXISTS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(),ErrorCode.AUTH_UNKNOWN);
        }

    }

    /**
     * 手机号码验证
     * @param phoneNum
     * @param code
     * @return
     */
    @RequestMapping(value="/validatePhone",method = RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Dto validatePhone(@RequestParam String phoneNum,@RequestParam String code){
        try {
            if(userService.validatePhone(phoneNum,code)){
                return DtoUtil.returnSuccess("手机验证成功");
            }else{
                return DtoUtil.returnSuccess("手机验证失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("验证失败",ErrorCode.AUTH_UNKNOWN);
        }
    }

    /**			 *
     * 合法E-mail地址：
     * 1. 必须包含一个并且只有一个符号“@”
     * 2. 第一个字符不得是“@”或者“.”
     * 3. 不允许出现“@.”或者.@
     * 4. 结尾不得是字符“@”或者“.”
     * 5. 允许“@”前的字符中出现“＋”
     * 6. 不允许“＋”在最前面，或者“＋@”
     */
    private boolean validEmail(String email){

        String regex="^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$"  ;
        return Pattern.compile(regex).matcher(email).find();
    }

    /**
     * 验证是否合法的手机号
     * @param phone
     * @return
     */
    private boolean validPhone(String phone) {
        String regex="^1[3578]{1}\\d{9}$";
        return Pattern.compile(regex).matcher(phone).find();
    }

}
